<?php

/**
 * proizvod actions.
 *
 * @package    marketing
 * @subpackage proizvod
 * @author     Your name here
 */
class proizvodActions extends sfActions
{
  public function executeIndex(sfWebRequest $request) {
        $filter = array(
            "naziv" => '',
            "nabavna_cena_od" => '',
            "nabavna_cena_do" => '',
            "kategorija_id" => '',
        );

        if ($request->hasParameter('proizvod')) {
            $this->getUser()->setAttribute('proizvod', $request->getParameter('proizvod'));
            $this->redirect('proizvod/index');
        }
        if ($this->getUser()->hasAttribute('proizvod')) {
            $filter = $this->getUser()->getAttribute('proizvod', $request->getParameter('proizvod'));
        }
        $this->filter = new ProizvodFormFilter();
        $this->filter->setDefaults($filter);
        $this->filter->getWidgetSchema()->setLabel('kategorija_id', 'kategorija');
        $c = new Criteria();
        $c1 = null;
        $c2 = null;
        if ($filter['naziv'] != '')
            $c->add(ProizvodPeer::NAZIV, "%" . $filter['naziv'] . "%", Criteria::LIKE);
        if ($filter['nabavna_cena_od'] != '')
            $c1 = $c->getNewCriterion(ProizvodPeer::NABAVNA_CENA, $filter['nabavna_cena_od'], Criteria::GREATER_EQUAL);
        if ($filter['nabavna_cena_do'] != '')
            $c2 = $c->getNewCriterion(ProizvodPeer::NABAVNA_CENA, $filter['nabavna_cena_do'], Criteria::LESS_EQUAL);
        if (isset($filter['kategorija_id']) && $filter['kategorija_id'] != '')
            $c->add(ProizvodPeer::KATEGORIJA_ID, $filter['kategorija_id'], Criteria::EQUAL);


        if ($c1 && $c2) {
            $c1->addAnd($c2);
            $c->add($c1);
        } else if ($c1)
            $c->add($c1);
        else if ($c2)
            $c->add($c2);

            $this->pager=new sfPropelPager('Proizvod', 3);
            $this->pager->setMaxPerPage(3);

            $this->pager->setPeerMethod('doSelectJoinAll');
            $this->pager->setPage($request->hasParameter('page')?$request->getParameter('page'):1);
            $this->pager->setCriteria($c);
            $this->pager->init();
            $this->proizvod_list = $this->pager->getResults();
            
    }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ProizvodForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new ProizvodForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($proizvod = ProizvodPeer::retrieveByPk($request->getParameter('id')), sprintf('Object proizvod does not exist (%s).', $request->getParameter('id')));
    $this->form = new ProizvodForm($proizvod);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($proizvod = ProizvodPeer::retrieveByPk($request->getParameter('id')), sprintf('Object proizvod does not exist (%s).', $request->getParameter('id')));
    $this->form = new ProizvodForm($proizvod);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($proizvod = ProizvodPeer::retrieveByPk($request->getParameter('id')), sprintf('Object proizvod does not exist (%s).', $request->getParameter('id')));
    $proizvod->delete();

    $this->redirect('proizvod/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $file = $this->form->getValue('slika');
      $proizvod = $form->save();
      $id   = $proizvod->getId();
      if ($file)
      {
        $filename = 'proizvod_'.$id;
        $extension = $file->getExtension($file->getOriginalExtension());
        $file->save(sfConfig::get('sf_upload_dir').'/images/proizvod/'.$filename.$extension);
      }

      $this->redirect('proizvod/index');
    }
  }
}
